package com.validate.platform.web.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by YangJiyu on 2017/8/1 0001.
 */
public class PropertyConstants {

    private static final Logger logger = LoggerFactory.getLogger(PropertyConstants.class);
    private static String sendURL;//发送地址

    /**
     * 获取发送地址
     *
     * @return
     */
    public static String getSendURL() {
        if (sendURL == null) {
            Properties properties =loadProps("sms.properties");
            sendURL = getString(properties, "sendURL");
        }
        return sendURL;
    }

    // 加载属性文件
    public static Properties loadProps(String propsPath) {
        Properties props = new Properties();
        InputStream is = null;
        try {
            String suffix = ".properties";
            if (propsPath.lastIndexOf(suffix) == -1) {
                propsPath += suffix;
            }
            is = PropertyConstants.class.getClassLoader().getResourceAsStream(propsPath);
            if (is != null) {
                props.load(is);
            }
        } catch (IOException e) {
            logger.error("加载属性文件出错！", e);
            throw new RuntimeException(e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                logger.error("释放资源出错！", e);
            }
        }
        return props;
    }

    public static String getString(Properties props, String key) {
        String value = "";
        if (props.containsKey(key)) {
            value = props.getProperty(key);
        }
        return value;
    }
}
